package com.dengtao.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengtao.common.R;
import com.dengtao.pojo.entity.VirusInsectTransmissionRelationships;
import com.dengtao.pojo.param.VirusInsectTransmissionRelationshipsQueryParam;
import com.dengtao.service.IVirusInsectTransmissionRelationshipsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 病毒-昆虫传播关系 前端控制器
 * </p>
 *
 * @author JiamingZhao
 * @since 2025-02-23
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@CrossOrigin
@RequestMapping("/virus-insect-transmission-relationships")
public class VirusInsectTransmissionRelationshipsController {

    private final IVirusInsectTransmissionRelationshipsService service;

    @PostMapping("/list")
    public R<IPage<VirusInsectTransmissionRelationships>> list(@RequestBody VirusInsectTransmissionRelationshipsQueryParam param) {
        String virusExistencePattern = param.getVirusExistencePattern();
        LambdaQueryWrapper<VirusInsectTransmissionRelationships> wrapper = Wrappers.<VirusInsectTransmissionRelationships>lambdaQuery();
        String keyWords = param.getKeyWords();
        if (StrUtil.isNotBlank(keyWords)) {
        wrapper.and(e -> e.like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVirusFamily, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVirusGenus, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVirusName, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVirusTaxId, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVectorOrder, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVectorFamily, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVectorGenus, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVector, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVectorTaxId, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getPubmed1, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getDoi1, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getEvidence1, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getVirusExistencePattern, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getEvidence2, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getPubmed2, keyWords)
                .or().like(StrUtil.isNotBlank(keyWords), VirusInsectTransmissionRelationships::getDoi2, keyWords));
        }
        if (StrUtil.isNotBlank(param.getVectorFamily())) {
            wrapper.eq(VirusInsectTransmissionRelationships::getVectorFamily, param.getVectorFamily());
        }
        if (StrUtil.isNotBlank(param.getVirusFamily())) {
            wrapper.eq(VirusInsectTransmissionRelationships::getVirusFamily, param.getVirusFamily());
        }

        if (StrUtil.isNotBlank(virusExistencePattern)) {
            if (Objects.equals(virusExistencePattern, "Unknow")) {
                //查询没有病毒模式的数据
                wrapper.eq(VirusInsectTransmissionRelationships::getVirusExistencePattern, "");
            } else {
                wrapper.eq(VirusInsectTransmissionRelationships::getVirusExistencePattern, virusExistencePattern);

            }
        }

        Page<VirusInsectTransmissionRelationships> page = new Page<>(param.getCurrent(), param.getSize());
        Page<VirusInsectTransmissionRelationships> relationshipsPage = service.page(page, wrapper);
        return R.success(relationshipsPage);
    }

}
